package com.schoolhelper.comment.service.impl;

import com.schoolhelper.comment.domain.Comment;
import com.schoolhelper.comment.mapper.CommentMapper;
import com.schoolhelper.comment.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    
    private static final Logger logger = LoggerFactory.getLogger(CommentServiceImpl.class);
    
    @Autowired
    private CommentMapper commentMapper;
    
    @Override
    public List<Comment> getCommentsByPostId(Long postId) {
        logger.info("获取帖子ID为{}的评论列表", postId);
        return commentMapper.getCommentsByPostId(postId);
    }
    
    @Override
    public List<Comment> getCommentsByPostId(Long postId, int page, int size) {
        logger.info("获取帖子ID为{}的评论列表，页码: {}, 每页数量: {}", postId, page, size);
        int offset = page * size;
        return commentMapper.getCommentsByPostIdWithPagination(postId, offset, size);
    }
    
    @Override
    @Transactional
    public Comment addComment(Comment comment) {
        logger.info("添加评论，帖子ID: {}, 用户ID: {}", comment.getPostId(), comment.getUserId());
        
        // 设置默认值
        comment.setParentId(comment.getParentId() == null ? 0L : comment.getParentId());
        comment.setCreateTime(new Date());
        comment.setUpdateTime(new Date());
        
        // 插入评论
        int result = commentMapper.insertComment(comment);
        if (result > 0) {
            // 更新帖子评论数
            commentMapper.incrementCommentCount(comment.getPostId());
            logger.info("评论添加成功，评论ID: {}", comment.getId());
            return comment;
        } else {
            throw new RuntimeException("评论添加失败");
        }
    }
    
    @Override
    @Transactional
    public boolean deleteComment(Long commentId, Long userId) {
        logger.info("删除评论，评论ID: {}, 用户ID: {}", commentId, userId);
        
        // 获取评论信息进行权限验证
        Comment comment = commentMapper.getCommentById(commentId);
        if (comment == null) {
            logger.warn("评论不存在，评论ID: {}", commentId);
            return false;
        }
        
        // 验证权限：只有评论作者可以删除自己的评论
        if (comment.getUserId() == null || !comment.getUserId().equals(userId)) {
            logger.warn("无权限删除评论，评论ID: {}, 评论作者: {}, 当前用户: {}", commentId, comment.getUserId(), userId);
            return false;
        }
        
        // 执行软删除
        int result = commentMapper.deleteComment(commentId);
        if (result > 0) {
            // 减少帖子评论数
            commentMapper.decrementCommentCount(comment.getPostId());
            logger.info("评论删除成功，评论ID: {}", commentId);
            return true;
        } else {
            logger.error("评论删除失败，评论ID: {}", commentId);
            return false;
        }
    }
}